package code1.binary_tree;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * 原题链接：https://leetcode.cn/problems/find-largest-value-in-each-tree-row/
 */
public class P515 {

    //求二叉树每一层的最大值
    public List<Integer> largestValues(TreeNode root) {
        List<Integer> result=new ArrayList<>();
        Queue<TreeNode> queue=new LinkedList<>();
        if(root==null){
            return result;
        }
        queue.offer(root);
        while(!queue.isEmpty()){
            int max=-Integer.MAX_VALUE-1;
            int size=queue.size();
            for(int i=0;i<size;i++){
                TreeNode node = queue.poll();
                max=Math.max(node.val,max);
                if(node.left!=null){
                    queue.offer(node.left);
                }
                if(node.right!=null){
                    queue.offer(node.right);
                }
            }
            result.add(max);
        }
        return result;
    }
}
